package com.gsk.europepharma.salesReport.struts.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRAdmPdtDAO;

public class ListeDivisions extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// VARIABLES
		HttpSession sessionObject = request.getSession();
		tbSpBoSRAdmPdtDAO admPdtDao;
		Session session;
		Query req;
		ArrayList result;
		Iterator iter;
		
		int cs_adm_pdt = 0;
		
		if (sessionObject.getAttribute("cs_adm_pdt") != null) {
			cs_adm_pdt = Integer.parseInt(sessionObject.getAttribute("cs_adm_pdt").toString());
		}
		
		// Recuperation du parametre 
		String cs_product_string = request.getParameter("cs_product");
		Integer cs_product = Integer.parseInt(cs_product_string);
		
		admPdtDao = new tbSpBoSRAdmPdtDAO();
		session = admPdtDao.getSession();
		req = session.getNamedQuery(tbSpBoSRAdmPdtDAO.QUERY_LISTE_DIVISIONS);
		req.setInteger("cs_product", cs_product);
		req.setInteger("cs_adm_pdt", cs_adm_pdt);
		result = new ArrayList(req.list());
		iter = result.iterator();
		JSONArray jsonArrayLines = new JSONArray();
		while (iter.hasNext()) {
			Object[] tmp = (Object[]) iter.next();
			JSONObject jsonObjectLine = new JSONObject();
			if (tmp[0] != null) {
				jsonObjectLine.put("cs_network", tmp[0].toString());
			} else {
				jsonObjectLine.put("cs_network", "");
			}
			if (tmp[1] != null) {
				jsonObjectLine.put("reseau", tmp[1].toString());
			} else {
				jsonObjectLine.put("reseau", "");
			}
			if (tmp[2] != null) {
				jsonObjectLine.put("cs_division", tmp[2].toString());
			} else {
				jsonObjectLine.put("cs_division", "");
			}
			if (tmp[3] != null) {
				jsonObjectLine.put("division", tmp[3].toString());
			} else {
				jsonObjectLine.put("division", "");
			}
			if (tmp[4] != null) {
				jsonObjectLine.put("cs_brand", tmp[4].toString());
			} else {
				jsonObjectLine.put("cs_brand", "");
			}
			if (tmp[5] != null) {
				jsonObjectLine.put("brand", tmp[5].toString());
			} else {
				jsonObjectLine.put("brand", "");
			}
			if (tmp[6] != null) {
				jsonObjectLine.put("cs_target", tmp[6].toString());
			} else {
				jsonObjectLine.put("cs_target", "");
			}
			if (tmp[7] != null) {
				jsonObjectLine.put("target", tmp[7].toString());
			} else {
				jsonObjectLine.put("target", "");
			}
			if (tmp[8] != null) {
				jsonObjectLine.put("checked", Boolean.parseBoolean(tmp[8].toString()));
			} else {
				jsonObjectLine.put("checked", false);
			}
			jsonArrayLines.add(jsonObjectLine);
		}
		
		HashMap hm = new HashMap();
		hm.put("data", jsonArrayLines);
		
		JSONObject json = JSONObject.fromObject(hm);
		response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();
		
		return null;
	}

}
